
// F. Chaxel 2010


#include <iostream>
using namespace std;

#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <syslog.h>
#include <string.h>

#include <asm/delay.h>

#include "App.h"

#include "SockTCP.h"
////////////////////////////////////////////////////////////////////////////
// IRobotAdvise interface implementation
////////////////////////////////////////////////////////////////////////////

void App::RobotStop(void)
{
	cout << "Event Stop"<<endl;
}
void App::MicaDataReceive(struct MicaPacket *Pkt)
{
	cout << "Event Receive Mica : ";
	if (Pkt!=NULL)
		cout << (int) Pkt->Start7e<<endl;
}
////////////////////////////////////////////////////////////////////////////
void App::Run()
{
	smote.Advise(this);	// to receive some call back messages
	robot.Advise(this);	// to receive some call back messages

	// not mounted at the same place time after time
	// so try the two expected positions ... some more experiments must be made 
	if (smote.Connect("/dev/ttyUSB2")==true)
		smote.StartRunThreaded();
	else
		if (smote.Connect("/dev/ttyUSB1")==true)
			smote.StartRunThreaded();
		else
			cout <<"Mica bridge accessibility problem"<<endl;

	robot.StartRunThreaded();

	SockTCP *trad = new SockTCP(&robot);
	trad->launch();

	// test
	cout << "comm" <<endl;
	robot.GoForward(0);
	robot.GoForward(50);

	// wait finished
	while (!robot.Is_Killed())
		sleep(1);

	delete trad;

	robot.UnAdvise(this);

	smote.UnAdvise(this);
	smote.Disconnect();
}
